
package org.jetnuke.dao.generic;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.LinksNewLink;

public class GenericLinkNewLinkDAO extends AutoKeys implements
		org.jetnuke.dao.LinkNewLinkDAO {

	/* (non-Javadoc)
	 * @see org.jetnuke.model.DownloadNewDownloadModel#retrieveByPK(int)
	 */
	public LinksNewLink retrieveByPK(int id) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("LinkNewLinkModel.selectByID"));
		p.setInt(1, id);
		
		ResultSet rs = p.executeQuery();
		LinksNewLink s = new LinksNewLink();
		
		if (rs.next()) {
			fillLinkNewLinkFromResultSet(s, rs);
		}
		
		rs.close();
		p.close();

		return s;
	}
	public void deleteByPK(int id) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("LinkNewLinkModel.deleteByID"));
		p.setInt(1, id);
		
		p.executeUpdate();
		p.close();
	}

	/**
	 * @param s
	 * @param rs
	 */
	protected void fillLinkNewLinkFromResultSet(LinksNewLink s, ResultSet rs)throws Exception {
		s.setCID(rs.getInt("CID"));
		s.setDESCRIPTION(rs.getString("DESCRIPTION"));
		s.setEMAIL(rs.getString("EMAIL"));
		//s.setFILESIZE(rs.getInt("FILESIZE"));
		//s.setHOMEPAGE(rs.getString("HOMEPAGE"));
		s.setLID(rs.getInt("LID"));
		s.setNAME(rs.getString("NAME"));
		//s.setSID(rs.getInt("SID"));
		s.setSUBMITTER(rs.getString("SUBMITTER"));
		s.setTITLE(rs.getString("TITLE"));
		s.setURL(rs.getString("URL"));
		//s.setVERSION(rs.getString("VERSION"));
		
	}

	public List selectAll() throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("LinkNewLinkModel.selectAll"));
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

	/**
	 * @param rs
	 * @return
	 */
	private List processSelectAll(ResultSet rs) throws Exception{
		List list = new ArrayList();
		while (rs.next()) {
			LinksNewLink u = new LinksNewLink();
			fillLinkNewLinkFromResultSet(u,rs);			
			list.add(u);
		}
		return list;
	}
	/* (non-Javadoc)
	 * @see org.jetnuke.model.DownloadNewDownloadModel#countAll()
	 */
	public int countAll() throws Exception {
		PreparedStatement p= JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("LinkNewLinkModel.countAll"));
		
		ResultSet rs = p.executeQuery();
		rs.next();
		int r= rs.getInt(1);
		rs.close();
		p.close();
		return r;
	}
	/* (non-Javadoc)
	 * @see org.jetnuke.model.LinkNewLinkModel#selectAllOrderLID()
	 */
	public List selectAllOrderLID() throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("LinkNewLinkModel.selectAll"));
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;

	}
	/* (non-Javadoc)
	 * @see org.jetnuke.model.LinkNewLinkModel#addNew(org.jetnuke.entities.LinksNewLink)
	 */
	public int addNew(LinksNewLink link) throws Exception{
		PreparedStatement p = this.getStatementForAutoKeys("LinkNewLinkModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals.getSql("LinkNewLinkModel.lastGeneratedLinkNewLinkID"));
		initNewLink(link,p);
		
		int id = this.executeAutoKeysQuery(p);
		p.close();		
		return id;
		
	}
	/**
	 * @param link
	 * @param p
	 */
	private void initNewLink(LinksNewLink link, PreparedStatement p)throws Exception {
		p.setInt(1,link.getCID());
		p.setString(2,link.getDESCRIPTION());
		p.setString(3,link.getEMAIL());
		p.setString(4,link.getNAME());
		p.setString(5,link.getSUBMITTER());
		p.setString(6,link.getTITLE());
		p.setString(7,link.getURL());
		
	}

}
